//
//  NetworkManager.m
//  ZhiHuDaily
//
//  Created by 吴宜昊 on 2022/2/4.
//

#import "NetworkManager.h"

@interface NetworkManager ()
@property(nonatomic,strong) NSString * strName;
@end
@implementation NetworkManager

- (instancetype) initWithBaseURL:(nullable NSString *) strURL {
    if (!strURL) {
        return nil;
    }
    self = [super initWithBaseURL: [NSURL URLWithString:strURL] ];
    if (!self) {
        return nil;
    }

    // 超时
    self.requestSerializer.timeoutInterval = 30.f;
    return self;
}

+(instancetype) shared {
    static NetworkManager * instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[NetworkManager alloc] initWithBaseURL:HttpBaseURL];
    });
    return instance;
}

#pragma mark - Request
+ (void) GET:(NSString *)URL
               parameters:(nullable NSDictionary *)parameters
                  success:(RequestSuccess)success
                  failure:(RequestFailed)failure {

    [NetworkManager.shared GET:URL parameters:parameters headers:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"%@",responseObject);
        success(responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failure(error);
    }];
}

+ (void) POST:(NSString *) URL
               parameters:(nullable NSDictionary *)parameters
                  success:(RequestSuccess)success
                  failure:(RequestFailed)failure {

    [NetworkManager.shared POST:URL parameters:parameters headers:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"%@",responseObject);
        success(responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failure(error);
    }];
}

@end
